<template>
  <div>
    <!-- data：表格的数据 -->
    <!-- row-key：用来渲染树形数据 -->
    <!-- default-expand-all：默认展开所有 -->
    <!-- tree-props：树形结构的数据 -->
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
    >
      <!-- prop:数据的字段 -->
      <!-- label:数据的文字 -->
      <!-- sortable：排序 -->
      <el-table-column prop="title" label="活动名称" width="180"> </el-table-column>
      <el-table-column label="开始时间" width="180">
        <template slot-scope="scope">
          <span>{{scope.row.begintime | formatTime}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="endtime" label="结束时间" width="180">
        <template slot-scope="scope">
          <span>{{scope.row.endtime | formatTime1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-tag type="success" v-show="scope.row.status === 1">正常</el-tag>
          <el-tag type="danger" v-show="scope.row.status === 2">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            circle
            @click="edit(scope.row)"
          ></el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            circle
            @click="del(scope.row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getSeckListFn, delMenuFn } from "../../apis";
import moment from 'moment'
export default {
  data() {
    return {
      // 表格的数据
      tableData: [],
    };
  },
  created() {
    // 发请求，获取菜单
    this.getMenuList();
  },
  // 局部注册过滤器
  filters: {
    formatTime(val) {
      let date = new Date(Number(val))
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    }
  },
  methods: {
    async getMenuList() {
      let res = await getSeckListFn();
      this.tableData = res.list;
    },
    edit(data) {
      // 子传父，打开弹窗并将data填入弹窗
      this.$emit('edit', data)
    },
    del(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 真的删除
          this.trueDel(id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async trueDel(id) {
      let res = await delMenuFn({id:id});
      if(res.code === 200) {
        // 更新数据
        this.tableData = res.list;
        // 提示成功
        this.$message({
          type: "success",
          message: "删除成功!",
        });
      }
    }
  },
};
</script>

<style>
</style>